﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Data;

namespace Yuanlin.Business
{
    public class CompanyVisitor : YuanlinUserControl,
        IReplyConverter<CompanyVisitorInfoReply, GetListOperation>
    {
        protected Repeater category;
        private EntityList companyVisitorInfos;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyVisitorInfoListRequest("", UserIdFromGreenNum,0,10));
        }

        protected override void OnLoad(EventArgs e)
        {
            category.ItemDataBound += new RepeaterItemEventHandler(category_ItemDataBound);
        }

        protected void category_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;

            CompanyVisitorInfo companyVisitorInfo = e.Item.DataItem as CompanyVisitorInfo;
            if (companyVisitorInfo == null)
                return;

            Repeater rptList = e.Item.FindControl("list") as Repeater;
            if (rptList == null)
                return;

            rptList.DataSource = BusinessManager.GetCompanyVisitorInfoList(RequestHelper.BuildGetCompanyVisitorInfoListRequest(companyVisitorInfo.Visitor.Value,0,0,10));
            rptList.DataBind();

        }

        private void BindData()
        {
            YuanlinUser user = UserManager.GetUser(UserIdFromGreenNum) as YuanlinUser;
            if (user == null)
                return;

            category.DataSource = companyVisitorInfos;
            category.DataBind();
        }

        #region IReplyConverter<CompanyVisitorInfoReply,GetListOperation> Members

        public void ConvertReply(CompanyVisitorInfoReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyVisitorInfos = reply.EntityList;
                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}
